/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testblind;

import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

/**
 *
 * @author Maciek
 */
public class PKW {
          public  RSAPublicKey pubKey;
          private  RSAPrivateKey privKey;
          
          public PKW(int keySize) throws NoSuchAlgorithmException{
                //generate the RSA key pair
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
            //initialise the KeyGenerator with a random number.
            keyGen.initialize(keySize, new SecureRandom());
            KeyPair keypair = keyGen.genKeyPair();
            privKey = (RSAPrivateKey)keypair.getPrivate();
            pubKey = (RSAPublicKey)keypair.getPublic();
          }
          
          public BigInteger sign(BigInteger number){
              BigInteger d = privKey.getPrivateExponent();
              BigInteger n = pubKey.getModulus();
              return number.modPow(d,n);
          }
          
          public boolean verifySignitureUsingPrivateKey(BigInteger signed, BigInteger notsinged){
              BigInteger d = privKey.getPrivateExponent();
              BigInteger n = pubKey.getModulus();
              return signed.equals(notsinged.modPow(d, n));
          }
          public BigInteger decode(BigInteger signed){
              return signed.modPow(pubKey.getPublicExponent(), pubKey.getModulus());
          }
}
